/**
 * 
 */
package com.jonosoft.photocast.viewer.web.client.ui;

import java.util.Date;
import java.util.HashMap;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.Widget;
import com.jonosoft.photocast.viewer.web.client.photocast.PhotocastItem;

/**
 * TODO Add description LibraryImage
 * (com.jonosoft.photocast.viewer.web.client.LibraryImage)
 * 
 * @author jon
 * 
 */
public class LibraryImage extends Composite implements SourcesClickEvents {


	public static final String STYLE_NAME = "library-image";
	
	private static final String WRAPPER_STYLE_NAME = STYLE_NAME;
	private static final String IMAGE_STYLE_NAME = "";

	private final SimplePanel wrapper = new SimplePanel();
	private final Image image = new Image();
	private String guid = null;
	private String imageUrl = null;
	private String title = null;
	private String description = null;
	private Date photoDate = null;
	private Date publishDate = null;
	private int displayType = -1; 	// 0: full-size
									// 1: small
									// 2: medium
									// 3: large
	private final HashMap routedClickListeners = new HashMap();

	public static LibraryImage createFromPhotocastItem(PhotocastItem photocastItem) {
		LibraryImage libraryImage = new LibraryImage();
		
		libraryImage.setGuid(photocastItem.getGuid());
		libraryImage.setImageUrl(photocastItem.getLink());
		libraryImage.setTitle(photocastItem.getTitle());
		libraryImage.setDescription(photocastItem.getDescription());
		libraryImage.setPhotoDate(photocastItem.getPhotoDate());
		libraryImage.setPublishDate(photocastItem.getPublishDate());
		
		return libraryImage;
	}
	
	public Object clone() {
		LibraryImage clonedLibraryImage = new LibraryImage();
		
		clonedLibraryImage.setGuid(guid);
		clonedLibraryImage.setImageUrl(imageUrl);
		clonedLibraryImage.setTitle(title);
		clonedLibraryImage.setDescription(description);
		
		if (photoDate != null)
			clonedLibraryImage.setPhotoDate((Date) photoDate.clone());
		
		if (publishDate != null)
			clonedLibraryImage.setPublishDate((Date) publishDate.clone());
		
		return clonedLibraryImage;
	}
	
	public LibraryImage() {
		initWidget(wrapper);
		
		wrapper.setWidget(image);
		wrapper.setStyleName(WRAPPER_STYLE_NAME);
		
		image.setStyleName(IMAGE_STYLE_NAME);
		
		setGuid("JI-" + hashCode());
    }
	
	public String getGuid() {
		return this.guid;
	}
	
	public String getImageUrl() {
		return this.imageUrl;
	}
	
	public String getTitle() {
		return this.title;
	}

	public String getDescription() {
		return this.description;
	}

	public Date getPhotoDate() {
		return this.photoDate;
	}

	public Date getPublishDate() {
		return this.publishDate;
	}
	
	public int getDisplayType() {
		return this.displayType;
	}
	
	public void setGuid(String guid) {
		this.guid = guid;
	}
	
	public void setImageUrl(String url) {
		this.imageUrl = url;
		setImageForDisplayType();
	}
	
    private void setImageForDisplayType() {
    	String url = getImageUrl();
    	
	    switch (displayType) {
	    	case 0:
	    		break;
	    	case 1:
	    		url += "?transform=small"; break;
	    	case 2:
	    		url += "?transform=medium"; break;
	    	case 3:
	    		url += "?transform=large"; break;
	    	default:
	    		return;	
	    }
	    
	    image.setUrl(url);
    }

	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}

	public void setPhotoDate(Date photoDate) {
    	this.photoDate = photoDate;
    }

	public void setPublishDate(Date publishDate) {
    	this.publishDate = publishDate;
	}
	
	public void setDisplayType(int displayType) {
		this.displayType = displayType;
		setImageForDisplayType();
	}

	public void addClickListener(ClickListener listener) {
		RoutedClickListener routedClickListener = (RoutedClickListener) routedClickListeners.get(listener);

		if (routedClickListener == null) {
			routedClickListener = new RoutedClickListener(listener, this);
			routedClickListeners.put(listener, routedClickListener);
			image.addClickListener(routedClickListener);
		}
	}

	public void removeClickListener(ClickListener listener) {
		image.removeClickListener(listener);
		routedClickListeners.remove(listener);
	}

	private class RoutedClickListener implements ClickListener {
		
		private ClickListener destinationClickListener = null;
		private LibraryImage senderObject = null;

		public RoutedClickListener(ClickListener destinationClickListener, LibraryImage senderObject) {
        	this.destinationClickListener = destinationClickListener;
        	this.senderObject = senderObject;
        }

		/**
         * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
         */
        public void onClick(Widget sender) {
	        destinationClickListener.onClick(senderObject);
        }
		
	}

}
